package com.medvalley.tcm.controller;

import com.medvalley.tcm.base.Result;
import com.medvalley.tcm.service.SMSService;
import com.medvalley.tcm.service.impl.AppointmentService;
import com.medvalley.tcm.vo.inputvo.AppointmentInputVo;
import com.medvalley.tcm.vo.inputvo.OrgStaffLoginInputVo;
import com.medvalley.tcm.vo.outputvo.AppointmentOutputVo;
import com.medvalley.tcm.vo.outputvo.OrgStaffOutputVo;
import com.medvalley.tcm.vo.outputvo.StaffAppointmentListOutputVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api("预约体检接口")
public class AppointmentController extends BaseController {

    @Autowired
    private SMSService smsService;

    @Autowired
    private AppointmentService appointmentService;

    @PostMapping("/staff/check")
    @ApiOperation("员工验证")
    public Result staffCheck(@RequestParam String staffName, @RequestParam String staffNo, @RequestParam int orgId) {
        OrgStaffOutputVo orgStaffOutputVo = appointmentService.checkStaff(staffName, staffNo, orgId);
        return success(orgStaffOutputVo);
    }

    @PostMapping("/staff/check_v2")
    @ApiOperation("员工验证")
    public Result staffCheckV2(@RequestParam String staffNo, @RequestParam int orgId) {
        OrgStaffOutputVo orgStaffOutputVo = appointmentService.checkStaffV2(staffNo, orgId);
        return success(orgStaffOutputVo);
    }

    @PostMapping("/appointment/do")
    @ApiOperation("预约")
    public Result appointment(@ModelAttribute AppointmentInputVo inputVo) {
        boolean result = smsService.validateCode(inputVo.getSickPhone(), inputVo.getSmsCode());
        if (!result) {
            return error("验证码错误");
        }
        AppointmentOutputVo appointmentOutputVo = appointmentService.doAppointment(inputVo);
        return success(appointmentOutputVo);
    }

    @PostMapping("/staff/login")
    @ApiOperation("员工登录")

    public Result staffLogin(@ModelAttribute OrgStaffLoginInputVo inputVo) {
        boolean result = smsService.validateCode(inputVo.getStaffPhone(), inputVo.getSmsCode());
        if (!result) {
            return error("验证码错误");
        }
        OrgStaffOutputVo orgStaffOutputVo = appointmentService.staffLogin(inputVo);
        return success(orgStaffOutputVo);
    }

    @PostMapping("/staff/appointment/list")
    @ApiOperation("业绩列表")
    public Result listAppointment(@RequestParam long staffId) {
        StaffAppointmentListOutputVo staffAppointmentListOutputVo = appointmentService.listAppointment(staffId);
        return success(staffAppointmentListOutputVo);
    }

    @GetMapping("/appointment/info")
    @ApiOperation("根据barcode获取预约信息")
    public Result getAppointmentInfo(@RequestParam String barcode) {
        AppointmentOutputVo appointmentOutputVo = appointmentService.getAppointmentInfo(barcode);
        return success(appointmentOutputVo);
    }

    @GetMapping("/appointment/check")
    @ApiOperation("验证barcode是否过期")
    public Result checkBarcode(@RequestParam String barcode) {
        if (appointmentService.checkBarcodeExpire(barcode)) {
            return error("条形码已过期,请重新预约");
        } else {
            return success();
        }

    }

}
